﻿/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SlotDuino.DAL;
using SlotDuino.Util;

namespace SlotDuino.UI.Pages
{
    /// <summary>
    /// Interaction logic for Preferences.xaml
    /// </summary>
    public partial class Preferences : Page
    {
        #region Dependency properties
        public static readonly DependencyProperty CompanyProperty = DependencyProperty.Register("Company", typeof(Company), typeof(Preferences));
        #endregion

        #region Private attributes
        private EntityContainer _context;
        #endregion      

        public Company Company
        {
            get { return (Company)GetValue(CompanyProperty); }
            set { SetValue(CompanyProperty, value); }
        }

        public Preferences()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Create the entity context
            _context = new EntityContainer();

            Company = _context.Companies.FirstOrDefault();
            if (null == Company)
            {
                Company = new Company();
            }

            btnSaveEdit.Content = "Atualizar";
            imgLogo.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(Company.Logo));
            txtName.Focus();
        }
     
        private void btnSaveEdit_Click(object sender, RoutedEventArgs e)
        {
           try
            {
                if (EntityState.Detached == Company.EntityState)
                {
                    _context.AddToCompanies(Company);
                }

                if ("" == txtName.Text)
                {
                    Msg.ShowError("Campo nome deve ser preenchido.");
                    txtName.Focus();
                }
                else
                {
                    _context.SaveChanges();

                    Msg.ShowOk("Preferências atualizadas com sucesso.");
                    NavigationService.GoBack();
                }
            }
            catch (Exception ex)
            {
                Msg.ShowError(ex.Message);
            } 
        }

        private void btnSearchLogoFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog SelectFile = new Microsoft.Win32.OpenFileDialog();
            Nullable<bool> result = SelectFile.ShowDialog();
            SelectFile.DefaultExt = ".JPG";
            SelectFile.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            if (result == true) 
            {
                txtLogo.Text = SelectFile.FileName;
                txtLogo.Focus();
                imgLogo.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(SelectFile.FileName));
            }
            
        }
    }
}
